package com.akkainaction.remote.serialize.java.server;

import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;
import akka.actor.UntypedActor;
import com.akkainaction.remote.serialize.java.po.User;
import com.typesafe.config.ConfigFactory;

import java.util.HashMap;
import java.util.Map;

public class UserServerActor extends UntypedActor {

    private Map<String, User> userMap = new HashMap<>();

    @Override
    public void preStart() throws Exception {
        userMap.put("zs", new User(13, "张三"));
        userMap.put("ls", new User(14, "李四"));
    }

    @Override
    public void onReceive(Object msg) throws Exception {
        if (msg instanceof String) {
            String key = (String) msg;
            System.out.println("UserServerActor 接收到的消息:"+msg);
            User user = userMap.get(key);
            getSender().tell(user, getSelf());
        } else {
            unhandled(msg);
        }
    }

    public static void main(String[] args) {
        ActorSystem system = ActorSystem.create("system", ConfigFactory.load("akka-remote-serialize-server.conf"));
        ActorRef actorRef = system.actorOf(Props.create(UserServerActor.class), "userServer");
    }
}
